package net.csdn.business.notify.handler.config;

import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.bean.ConsumerBean;
import com.aliyun.openservices.ons.api.bean.ProducerBean;
import com.aliyun.openservices.ons.api.bean.Subscription;
import com.aliyun.openservices.ons.api.MessageListener;
import com.aliyun.openservices.shade.com.google.common.collect.Maps;
import lombok.Data;
import net.csdn.business.notify.handler.listener.DirectMessageListener;
import net.csdn.business.notify.handler.listener.PrivateMessageListener;
import net.csdn.business.notify.handler.listener.RemindMessageListener;
import net.csdn.business.notify.handler.service.IMqConsumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import java.util.Map;
import java.util.Optional;
import java.util.Properties;

/**
 * @ClassName MqConfig
 * @Description rocketmq配置
 * @Author zhangyl
 * @Date 2023/5/5 18:44
 */
@Data
@Configuration
@RefreshScope
public class MqConfig {
    private static final String DEFAULT_MAX_TIMES = "5";
    private static final String DEFAULT_THREAD_NUMS = "5";

    @Value("${ali.mq.accessKey}")
    private String accessKey;

    @Value("${ali.mq.secretKey}")
    private String secretKey;

    @Value("${ali.mq.tcp.nameSrvAddr}")
    private String tcpNameSrvAddr;


    /*@Value("${ali.mq.notify.groupId:GID_WEB_NOTIFY_HANDLER}")
    private String notifyHandlerGroupId;*/

    @Value("${ali.mq.notify.remind.groupId:GID_WEB_NOTIFY_REMIND_HANDLER}")
    private String notifyRemindHandlerGroupId;

    @Value("${ali.mq.notify.private.groupId:GID_WEB_NOTIFY_PRIVATE_HANDLER}")
    private String notifyPrivateHandlerGroupId;

    @Value("${ali.mq.notify.direct.groupId:GID_WEB_NOTIFY_DIRECT_HANDLER}")
    private String notifyDirectHandlerGroupId;


    @Value("${ali.mq.instanceId}")
    private String instanceId;

    @Value("${ali.mq.notify.remind.topic:NOTIFY_REMIND_CHANNEL}")
    private String remindTopic;

    @Value("${ali.mq.notify.private.topic:NOTIFY_PRIVATE_CHANNEL}")
    private String privateTopic;

    /**
     * 营销类直达消息
     */
    @Value("${ali.mq.notify.direct.topic:NOTIFY_DIRECT_CHANNEL}")
    private String directTopic;

    @Value("${ali.mq.consumer.max:5}")
    private String consumerMaxTimes;
    @Value("${ali.mq.consumer.thread.num:5}")
    private String consumerThreadNums;

    @DependsOn("remindMessageListener")
    @Bean(name = "remindMessageConsumer", initMethod = "start", destroyMethod = "shutdown")
    public ConsumerBean initRemindConsumer(RemindMessageListener remindMessageListener) {
        ConsumerBean remindConsumer = new ConsumerBean();
        Properties properties=getMqProperties();
        properties.setProperty(PropertyKeyConst.GROUP_ID, notifyRemindHandlerGroupId);
        properties.setProperty(PropertyKeyConst.MaxReconsumeTimes, Optional.ofNullable(consumerMaxTimes).orElse(DEFAULT_MAX_TIMES));
        // 默认 20-64
        properties.setProperty(PropertyKeyConst.ConsumeThreadNums, Optional.ofNullable(consumerThreadNums).orElse(DEFAULT_THREAD_NUMS));
        remindConsumer.setProperties(properties);
        //订阅关系
        Map<Subscription, MessageListener> subscriptionTable = Maps.newHashMap();
        Subscription subscription = new Subscription();
        subscription.setTopic(remindTopic);
        //subscription.setExpression(projectTag);
        subscriptionTable.put(subscription, remindMessageListener);
        remindConsumer.setSubscriptionTable(subscriptionTable);
        return remindConsumer;
    }


    @DependsOn("privateMessageListener")
    @Bean(name = "privateMessageConsumer", initMethod = "start", destroyMethod = "shutdown")
    public ConsumerBean initPrivateConsumer(PrivateMessageListener privateMessageListener) {
        ConsumerBean remindConsumer = new ConsumerBean();
        Properties properties=getMqProperties();
        properties.setProperty(PropertyKeyConst.GROUP_ID, notifyPrivateHandlerGroupId);
        properties.setProperty(PropertyKeyConst.MaxReconsumeTimes, Optional.ofNullable(consumerMaxTimes).orElse(DEFAULT_MAX_TIMES));
        // 默认 20-64
        properties.setProperty(PropertyKeyConst.ConsumeThreadNums, Optional.ofNullable(consumerThreadNums).orElse(DEFAULT_THREAD_NUMS));
        remindConsumer.setProperties(properties);
        //订阅关系
        Map<Subscription, MessageListener> subscriptionTable = Maps.newHashMap();
        Subscription subscription = new Subscription();
        subscription.setTopic(privateTopic);
        //subscription.setExpression(projectTag);
        subscriptionTable.put(subscription, privateMessageListener);
        remindConsumer.setSubscriptionTable(subscriptionTable);
        return remindConsumer;
    }


    @DependsOn("directMessageListener")
    @Bean(name = "directMessageConsumer", initMethod = "start", destroyMethod = "shutdown")
    public ConsumerBean initDirectConsumer(DirectMessageListener directMessageListener) {
        ConsumerBean remindConsumer = new ConsumerBean();
        Properties properties=getMqProperties();
        properties.setProperty(PropertyKeyConst.GROUP_ID, notifyDirectHandlerGroupId);
        properties.setProperty(PropertyKeyConst.MaxReconsumeTimes, Optional.ofNullable(consumerMaxTimes).orElse(DEFAULT_MAX_TIMES));
        // 默认 20-64
        properties.setProperty(PropertyKeyConst.ConsumeThreadNums, Optional.ofNullable(consumerThreadNums).orElse(DEFAULT_THREAD_NUMS));
        remindConsumer.setProperties(properties);
        //订阅关系
        Map<Subscription, MessageListener> subscriptionTable = Maps.newHashMap();
        Subscription subscription = new Subscription();
        subscription.setTopic(directTopic);
        //subscription.setExpression(projectTag);
        subscriptionTable.put(subscription, directMessageListener);
        remindConsumer.setSubscriptionTable(subscriptionTable);
        return remindConsumer;
    }

    private Properties getMqProperties() {
        Properties properties = new Properties();
        properties.setProperty(PropertyKeyConst.AccessKey, this.accessKey);
        properties.setProperty(PropertyKeyConst.SecretKey, this.secretKey);
        properties.setProperty(PropertyKeyConst.NAMESRV_ADDR, this.tcpNameSrvAddr);
        return properties;
    }

}